//获取应用实例
var app = getApp()
Page({
  data: {
    // start:'开始',
    seckillshow:0,
    page:0,
    days:'00',
    hours:'00',
    minutes:'00',
    seconds:'00',
  }, 
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    that.requestIndex();
  },
  requestIndex: function () {
    var that = this;
    // var page = that.data.page;
    //发起请求，获取列表列表
    // wx.showToast({
    //   title: '加载中',
    //   icon: 'loading',
    //   mask: true,
    //   duration: 10000
    // });
    wx.request({
      url: app.globalData.domin+'/applet.php',
      data: {
        map: 'applet_mall_limit_list',
        suid: app.globalData.suid,
        version: app.globalData.version,
        base: app.globalData.base
      },
      success: function (res) {
        console.log(res.data.data);
        console.log(res.data.data[0].goodsList);
        if (res.data.ec == 200) {
          var startTime= res.data.data[0].startTime;
          var end = res.data.data[0].endTime;
          that.countDjs(startTime, end);
          that.setData({
            seckillData: res.data.data,
            systemImg: res.data.data[0].img,
            laid: res.data.data[0].id,
            status: res.data.data[0].status,
            statusNote: res.data.data[0].statusNote,
            startTime: res.data.data[0].startTime,
            end: res.data.data[0].endTime,
            seckillshow: res.data.data[0].id,
            goodsList: res.data.data[0].goodsList,
          })
          // var start = that.data.start;
          // var end = that.data.end;
          // that.countDjs(start, end);
          // var sharedata = {
          //   shareOpen: res.data.data.shareOpen,
          //   shareUrl: res.data.data.shareUrl,
          //   shareTitle: res.data.data.template.title
          // }
          // app.globalData.sharedata = sharedata;
          // // 存储是否开启客服
          // app.globalData.customerService = res.data.data.customerService;
          // // 设置电话
          // app.globalData.telphone = res.data.data.mobile;
          // app.globalData.watermark = res.data.data.watermark;
          // var namelogo = {
          //   name: res.data.data.name,
          //   logo: res.data.data.logo
          // };
          // app.globalData.namelogo = namelogo;
          // wx.setNavigationBarTitle({
          //   title: res.data.data.template.title
          // });
          // var shopname = res.data.data.name;
          // var allArr = [];
          // var initArr = that.data.shopGoods;
          // var curArr = res.data.data.goods;
          // var lastPageLength = curArr.length;
          // if (page > 0) {
          //   allArr = initArr.concat(curArr);
          // } else {
          //   allArr = res.data.data.goods;
          // }
          // that.setData({
          //   shopGoods: allArr
          // })
          // if (lastPageLength < 10) {
          //   that.setData({
          //     noMoretip: true,
          //     showLoading: false
          //   });
          // }
          // console.log(that.data);
          // wx.setStorage({
          //   key: "shopInfoname",
          //   data: shopname,
          //   success: function (e) {
          //     console.log("存储成功")
          //   }
          // })
        } else {
          // console.log(res.data)
          // if (page <= 0) {
          //   that.setData({
          //     shopGoods: []
          //   })
          // } else {
          //   that.setData({
          //     noMoretip: true,
          //     showLoading: false
          //   });
          // }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  toggleBtn:function(e){
    var that = this;
    clearInterval(that.data.intervarID);
    var detail = e.currentTarget.dataset.detail;
    var toggleId = detail.id;
    var startTime = detail.startTime;
    var endTime = detail.endTime;
    var laid = detail.id;
    console.log(detail);
    console.log(startTime);
    console.log(endTime);
    that.setData({
      seckillshow: toggleId,
      page: 0,
      systemImg: detail.img,
      status: detail.status,
      end: endTime,
      startTime: startTime,
      goodsList: detail.goodsList,
      laid:laid
    });
    that.countDjs(startTime, endTime);
  },
  countDjs: function (start,end){
    var that = this;
    // console.log(start);
    // console.log(end);
    var startTime = start*1000;
    var endTime = end*1000;
    var curtime = new Date();  //避免页面渲染倒计时的时候出现一秒的延迟。
    var startSpan1 = startTime - curtime;
    var endSpan2 = endTime - curtime;
    if (startSpan1 > 0) {
      that.setData({
        start: '开始',
      })
      that.djs(startSpan1);
    } else {
      that.setData({
        start: '结束',
      })
      that.djs(endSpan2);
    }
    that.data.intervarID = setInterval(function () {
      var nowTime = new Date();
      var startSpan = startTime-nowTime;
      var endSpan = endTime - nowTime;
      if (startSpan>0){
        that.setData({
          start: '开始',
        })
        that.djs(startSpan);
      }else{
        that.setData({
          start:'结束',
        })
        that.djs(endSpan);
      }
    },1000)
  },
  djs:function(time){
    var that = this;
      // var leftTime = (new Date("2017/09/13 17:59:59")) - (new Date()); //计算剩余的毫秒数 
      var leftTime=time;
      that.setData({
        leftTime: leftTime
      })
      // console.log(leftTime);
      if (leftTime>0){
        var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 
        var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
        var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟 
        var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 
        days = days < 10 ? '0' + days : days;
        // console.log(days);
        hours = hours < 10 ? '0' + hours : hours;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        seconds = seconds < 10 ? '0' + seconds : seconds;
        that.setData({
          days: days,
          hours: hours,
          minutes: minutes,
          seconds: seconds
        })
        if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
          clearInterval(that.data.intervarID);
        }
      }else{
        that.setData({
          days: '00',
          hours: '00',
          minutes:'00',
          seconds: '00'
        })
      }
  },
  opendetail:function(e){
    var that = this;
    var goodId = e.currentTarget.dataset.id;
    var endTime = e.currentTarget.dataset.endtime;
    var startTime = e.currentTarget.dataset.starttime;
    var laid = e.currentTarget.dataset.laid;
    console.log(laid);
    console.log(endTime);
    if (!goodId) {
      return;
    }
    wx.navigateTo({
      url: '/subpages1/goodDetail/goodDetail?id=' + goodId + '&endTime=' + endTime + '&laid=' + laid + '&startTime=' + startTime
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
    });
    that.onLoad();
    console.log("下拉刷新");
  },
  // onReachBottom: function () {
  //   var that = this;
  //   console.log("到达页面底部")
  //   var isMore = that.data.noMoretip;
  //   var page = that.data.page;
  //   page++;
  //   that.setData({
  //     page: page
  //   });
  //   if (isMore) {
  //     console.log("已完成或正在加载");
  //   } else {
  //     that.styleList();
  //   }
  // }
  onShareAppMessage: function () {
    var that = this;
    var title = '今日秒杀';
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages1/seckill/seckill?enterfrom=share'
    }
  }
})
